package com.rw.controller;

import com.rw.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "CheckMoneyServlet", value = "/checkMoney")
public class CheckMoneyServlet extends HttpServlet {

    private AccountService accountService;

    @Override
    public void init() throws ServletException {
        super.init();
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
        this.accountService = (AccountService) ac.getBean("accountService");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        //获取参数
        Integer aid = Integer.parseInt(request.getParameter("aid"));
        Double money = Double.parseDouble(request.getParameter("money"));
        //在service进行校验
        try {
            boolean flag = this.accountService.getCheckMoney(aid, money);
            if (flag){
                response.getWriter().println("true");
            }else {
                response.getWriter().println("false");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
}
